#!/bin/sh

source scripts/common.sh

####################################################################################################

PAINT_VERSION=${DRISHTI_VERSION}
PAINT_BASENAME="Paint"
PAINT_DIR="../../tools/paint"

PAINT_APP_BUNDLE_DIR=${BUILD_PRODUCTS_DIR}/${PAINT_BASENAME}.app
PAINT_APP_FRAMEWORK_DIR=${PAINT_APP_BUNDLE_DIR}/Contents/Frameworks

####################################################################################################

pushd ${PAINT_DIR} || exit 1

####################################################################################################

report "Configuring ''${PAINT_BASENAME}-${PAINT_VERSION}'..."

${THIRD_PARTY_QT_DIR}/bin/qmake "CONFIG += x86 ppc x86_64" QT_VERSION="4.6.2" || exit 
if test $? -ne 0; then
        echo "configure failed"
        exit 1
fi

####################################################################################################

report "Building '${PAINT_BASENAME}-${PAINT_VERSION}'..."

make clean || exit 1
nice make -j8 || exit 1
nice make install || exit 1

####################################################################################################

popd
pushd ${BUILD_PRODUCTS_DIR} || exit -1

####################################################################################################

report "Installing dependencies..."

rm -rf $PAINT_APP_FRAMEWORK_DIR || exit -1
mkdir -p $PAINT_APP_FRAMEWORK_DIR || exit -1

install_dylib $PAINT_BASENAME "Frameworks" "${THIRD_PARTY_QT_DIR}/lib/libQtCLucene.4.6.2.dylib" || exit 1
install_dylib $PAINT_BASENAME "Frameworks" "/usr/local/lib/libjpeg.62.dylib" || exit -1
install_dylib $PAINT_BASENAME "Frameworks" "/usr/local/lib/libpng12.0.dylib" || exit -1

for fw in ${QT_FRAMEWORK_LIST}; do
  install_qt_framework $PAINT_BASENAME $fw
done

for dp in ${QT_FRAMEWORK_LIST}; do
for fw in ${QT_FRAMEWORK_LIST}; do
  localise_qt_framework $PAINT_BASENAME $fw $dp
done

localise_dylib $PAINT_BASENAME "Frameworks" $dp "${THIRD_PARTY_QT_DIR}/lib/libQtCLucene.4.6.2.dylib" || exit 1
localise_dylib $PAINT_BASENAME "Frameworks" $dp "/usr/local/lib/libjpeg.62.dylib" || exit -1
localise_dylib $PAINT_BASENAME "Frameworks" $dp "/usr/local/lib/libpng12.0.dylib" || exit -1

done

####################################################################################################

report "Installing QT Plugins..."
report ""

install_qt_plugins $PAINT_BASENAME

report "Creating QT Config..."
report ""

create_qt_conf $PAINT_BASENAME

####################################################################################################

popd

####################################################################################################

report "Installing resources..."
report ""

cp resources/DrishtiPaintInfo.plist ${PAINT_APP_BUNDLE_DIR}/Contents/Info.plist || exit -1 
mkdir -p ${PAINT_APP_BUNDLE_DIR}/Contents/Resources 
cp  resources/DrishtiPaint.icns ${PAINT_APP_BUNDLE_DIR}/Contents/Resources || exit -1

mv ${PAINT_APP_BUNDLE_DIR}/Contents/MacOS/${PAINT_BASENAME} ${PAINT_APP_BUNDLE_DIR}/Contents/MacOS/Drishti${PAINT_BASENAME} || exit -1
mv "${PAINT_APP_BUNDLE_DIR}" "${BUILD_PRODUCTS_DIR}/Drishti Paint.app" || exit -1

report "Done!"

####################################################################################################


